package sm.upgrade.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sm.upgrade.DBDictUpgradeImpl;
import sm.upgrade.api.DBDictUpgrade;
import sm.upgrade.api.UpgradePhase;
import sm.upgrade.util.UpgradeContext;

public class UpgradeDBDictMain {

	private static Logger logger = LoggerFactory.getLogger(DBDictUpgrade.class);

	public static void main(String[] args) {
		boolean fix = true;
		int argsLength = args.length;
		if (argsLength == 1) {
			String fixStr = args[0].trim();
			if ("nofix".equalsIgnoreCase(fixStr)) {
				fix = false;
			}
		}

		UpgradeContext context = UpgradeContext.getInstance();
		ClassPathXmlApplicationContext customerContext = context
				.getOrCreateCustomerContext();

		UpgradePhase upgrade = (UpgradePhase) customerContext
				.getBean("deleteInboxGLUpgrade");
		upgrade.upgrade();

		upgrade = (UpgradePhase) customerContext.getBean("severityUpgrade");
		upgrade.upgrade();

		upgrade = (UpgradePhase) customerContext.getBean("priorityUpgrade");
		upgrade.upgrade();

		upgrade = (UpgradePhase) customerContext.getBean("cm3rUpgrade");
		upgrade.upgrade();

		DBDictUpgrade dbdictUpgrade = new DBDictUpgradeImpl();
		try {
			// upgrade the dbdict for upgrade process first
			dbdictUpgrade.upgradeDBDict(UpgradePhase.UPGRADE_RESULTS, fix);
			context.dropCustomer();

			// upgrade other dbdicts
			dbdictUpgrade = new DBDictUpgradeImpl();
			dbdictUpgrade.upgradeDBDict(fix);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

}
